import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-04-19
 * Time: 16:17
 */
class TreeNode{
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val){
        this.val = val;
    }
}
public class BinaryTree {
    // 创建二叉树
    public TreeNode create(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;
        return A;// 返回一个根节点

    }
    // 二叉树的遍历
    // 前序遍历 根左右
    public void preOrder(TreeNode root){
        if (root == null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历 左根右
    public void inOrder(TreeNode root){
        if (root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    // 后序遍历 左右根
    public void postOrder(TreeNode root){
        if (root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    // 获取树中节点的个数
    public int count = 0;
    public int size(TreeNode root){
        if (root == null){
            return 0;
        }
        count++;
        size(root.left);
        size(root.right);

        return count;
    }

    // 子问题思路 求节点个数
    public int getSize(TreeNode root){
        if (root == null){
            return 0;
        }
        return getSize(root.right)+ getSize(root.left)+1;
    }

    // 获取叶子节点个数  遍历思路
    public int leafCount = 0;
    public int leafNode(TreeNode root){
        if (root == null){
            return 0;
        }
        leafNode(root.left);
        leafNode(root.right);
        if (root.right == null && root.left == null){
            leafCount++;
        }
        return leafCount;
    }

    /**
     * 获取叶子节点的个数
     * 子问题思路
     * @param root
     * @return
     */
    int getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            //当前的root是叶子节点
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    // 获取第k层节点个数
    int getKLevelNodeCount(TreeNode root,int k) {
        if (root == null){
            return 0;
        }
        if (k == 1){
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }


    // 获取二叉树高度
    public int getHigh(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftGetHigh = getHigh(root.left);
        int rightGetHigh = getHigh(root.right);
        return leftGetHigh > rightGetHigh ? leftGetHigh+1 :rightGetHigh+1;
    }

    // 子问题思路
    int getHeight2(TreeNode root) {
        if(root == null) return 0;
        return getHeight2(root.left) > getHeight2(root.right) ?
                getHeight2(root.left)+1 : getHeight2(root.right)+1;
    }

    // 检查value值是否存在
    TreeNode find(TreeNode root,char val){
        if(root == null) return null;
        if(root.val == val) return root;


        TreeNode ret = find(root.left,val);
        if(ret != null) {
            return ret;
        }
        ret = find(root.right,val);
        if(ret != null) {
            return ret;
        }
        return null;
    }
    // 判断是否为完全二叉树

    /**
     * 是不是完全二叉树
     * @param root
     * @return
     */
    boolean isCompleteTree(TreeNode root) {
        if(root == null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }

        while (!queue.isEmpty()) {
            TreeNode top = queue.peek();
            if(top != null) {
                return false;
            }
            queue.poll();
        }
        return true;
    }


}
